package com.tools.cleanmaster.utils;

/**
 * desc:
 * date: 2018/6/21
 * author: ancun
 */

public class ColorPickGradientUtil {

    //设置的颜色
    private static final int[] DEFAULT_COLORS = new int[]{
            0xFF5a6dff,
            0xFF5a6dff,
            0xFF24b66d,
            0xFFe3a000,
            0xFFFB8061,
            0xFFF7596B
    };

    //每个颜色的位置
    private static final float[] DEFAULT_POSITIONS = new float[]{0f, 0.2f, 0.50f, 0.75f, 0.95f, 1f};

    /**
     * @param colors    获取某个百分比位置的颜色
     * @param positions
     * @param radio     取值[0,1]
     * @return
     */
    public static int getColor(int[] colors, float[] positions, float radio) {

        int startColor;
        int endColor;
        if (radio >= 1) {
            return colors[colors.length - 1];
        }
        for (int i = 0; i < positions.length; i++) {
            if (radio <= positions[i]) {
                if (i == 0) {
                    return colors[0];
                }
                startColor = colors[i - 1];
                endColor = colors[i];
                float areaRadio = getAreaRadio(radio, positions[i - 1], positions[i]);
                return getColorFrom(startColor, endColor, areaRadio);
            }
        }
        return -1;
    }

    /**
     * 获取某个百分比位置的颜色
     *
     * @param radio 取值[0,1]
     * @return
     */
    public static int getColor(float radio) {
        return getColor(DEFAULT_COLORS, DEFAULT_POSITIONS, radio);
    }

    private static float getAreaRadio(float radio, float startPosition, float endPosition) {
        return (radio - startPosition) / (endPosition - startPosition);
    }

    /**
     * 取两个颜色间的渐变区间 中的某一点的颜色
     *
     * @param startColor
     * @param endColor
     * @param radio
     * @return
     */
    private static int getColorFrom(int startColor, int endColor, float radio) {
        /*int redStart = Color.red(startColor);
        int blueStart = Color.blue(startColor);
        int greenStart = Color.green(startColor);
        int redEnd = Color.red(endColor);
        int blueEnd = Color.blue(endColor);
        int greenEnd = Color.green(endColor);

        int red = (int) (redStart + ((redEnd - redStart) * radio + 0.5));
        int greed = (int) (greenStart + ((greenEnd - greenStart) * radio + 0.5));
        int blue = (int) (blueStart + ((blueEnd - blueStart) * radio + 0.5));
        return Color.argb(255, red, greed, blue);*/

        int startInt = startColor;
        int startA = (startInt >> 24);
        int startR = (startInt >> 16) & 0xff;
        int startG = (startInt >> 8) & 0xff;
        int startB = startInt & 0xff;

        int endInt = endColor;
        int endA = (endInt >> 24);
        int endR = (endInt >> 16) & 0xff;
        int endG = (endInt >> 8) & 0xff;
        int endB = endInt & 0xff;

        return (startA + (int)(radio * (endA - startA))) << 24 |
                (startR + (int)(radio * (endR - startR))) << 16 |
                (startG + (int)(radio * (endG - startG))) << 8 |
                (startB + (int)(radio * (endB - startB)));
    }

}
